Skip to content

Bind a variable only once PHP has assigned it - #877

Draft
Firehed wants to merge 7 commits into
mainfrom
870/foreach-rebinds-subject
Draft

Firehed wants to merge 7 commits into
mainfrom
870/foreach-rebinds-subject

Conversation

@Firehed

@Firehed Firehed commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #870.

VariableBindings decided what is bound at a position by where each statement starts. Each kind of binding actually takes effect at a different point, so each kind got its own check, added only after a crash exposed it (#869 for assignments). This replaces the per-kind checks with one rule: a variable is bound once the code PHP runs before assigning it has finished before the position. Each binding kind names that code:

  • Assignment: the whole assignment, since the right side is evaluated first. Same behavior as Bind an assignment only once it is evaluated #869.
  • Foreach key and value: the subject, which PHP evaluates once before assigning them. Previously these were bound from the start of the foreach, so in foreach ($x as $x) resolving the subject found the loop's own $x, which resolved the subject again, recursing until the server crashed (A foreach over a variable it rebinds must not crash the server #870). Hovering $users->getName() inside foreach ($users as $users) reproduced it as a segfault.
  • Catch variable: the catch header, which ends with the variable. Previously every catch variable was bound from the start of the try statement, so inside the try body $e already counted as bound.

Parameters and use variables are unchanged: they are bound before the body runs.

Each case has a unit test in VariableBindingsTest. #869's existing assignment test passes unchanged. A catch without a variable now also has a test.

There is no end-to-end case. With the crash gone, hover inside such a loop answers with nothing, because the element type of a loop over a variable is never resolved: element types are currently read only from the docblock of a call or property being iterated directly. That limitation predates this change and is out of scope.

Not covered: assignments nested inside expressions, such as if ($user = find()), are still never bound. That is a missing binding kind rather than a timing problem.

🤖 Generated with Claude Code

Firehed and others added 4 commits October 28, 2026 10:09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Firehed
Firehed marked this pull request as ready for review October 28, 2026 17:33
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.64%. Comparing base (1a778ad) to head (ed69b57).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #877   +/-   ##
=========================================
  Coverage     99.64%   99.64%           
- Complexity     1969     1971    +2     
=========================================
  Files           145      145           
  Lines          4513     4515    +2     
=========================================
+ Hits           4497     4499    +2     
  Misses           16       16           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Firehed
Firehed marked this pull request as draft October 28, 2026 17:56
Firehed and others added 3 commits October 28, 2026 10:56
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Firehed Firehed changed the title Bind foreach variables only after the subject Bind a variable only once PHP has assigned it Oct 28, 2026
@Firehed

Firehed commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

Parked; not ready to merge.

The branch's current head (ed69b57e) introduces a regression: hover on the variable in a catch (E $e) header no longer finds it, because the catch variable binds only after its own end. Do not merge as is.

Open question before resuming: the per-kind visibility checks in VariableBindings::walk (#869's, and this branch's) treat a symptom. A binding does not record when it takes effect, and ExpressionResolver::typeOfBinding re-enumerates the binding kinds from syntax on its own. Fixing that structure would also cover the catch variable being visible inside the try body, a write target ($x in $x = 1) resolving to the earlier binding, and assignments nested in expressions never being bound.

Options when this is picked back up:

  • Return to the foreach-only fix at ea32c499 (same shape as Bind an assignment only once it is evaluated #869), add a resolver-level test that the crash is gone and a test for the foreach key's timing, and track the structural problem in one root-cause issue.
  • Or take on the structural fix here and replace the per-kind checks.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A foreach over a variable it rebinds must not crash the server

1 participant